# -*- coding:utf-8 -*-
# File name : rule_agent.py
# Create date : 2022/11/1

from mozi_simu_sdk import geo


class CAgent:
    def __init__(self, side):
        self.flag = True
        # 获取推演方条令对象
        self.side_doct = side.get_doctrine()
        # 设置自动规避为“否”
        self.side_doct.evade_automatically('false')
        # 设置电磁管控-雷达为“静默”
        self.side_doct.set_em_control_status('Radar', 'Passive')
        # 设置武器使用规则：RIM-162E
        self.side_doct.set_weapon_release_authority('3138', '2203', 1, 1, 'inherit', 15, 'false')
        # 设置武器使用规则：RIM-174A
        self.side_doct.set_weapon_release_authority('1310', '2000', 'none', 'inherit', 'inherit', 'inherit', 'false')
        self.side_doct.set_weapon_release_authority('1310', '2200', 1, 1, 'inherit', 'inherit', 'false')
        self.side_doct.set_weapon_release_authority('1310', '2203', 1, 1, 'inherit', 'inherit', 'false')
        # 设置武器使用规则：RIM-66M-2
        self.side_doct.set_weapon_release_authority('1195', '2201', 'inherit', 'inherit', 40, 'inherit', 'false')
        # 获取E-1预警机活动单元对象
        self.awacs = side.get_unit_by_guid('2fa6f502-cc50-4ab8-85f9-8c40c760689f')
        # 规划预警机航路点
        self.awacs.plot_course([(-16.543056, -26.594930)])
        # 调整预警机电磁管控
        self.awacs.get_doctrine().set_emcon_according_to_superiors('no')
        self.awacs.get_doctrine().set_em_control_status('Radar', 'Active')
        # 获取G-1型护卫舰 #3活动单元对象
        self.ffg_3 = side.get_unit_by_guid('4a4fca56-5974-4df1-8266-fbbc0946c62a')
        # 规划G-1型护卫舰 #3航路点
        self.ffg_3.plot_course([(-16.543056, -26.594930)])
        # 获取G-1型护卫舰 #4活动单元对象
        self.ffg_4 = side.get_unit_by_guid('b61caa7f-3b17-4e0e-9f79-2eb2a5b3911b')
        # 规划G-1型护卫舰 #4航路点
        self.ffg_4.plot_course([(-16.543056, -26.594930)])

    def step(self, side):
        # 无人舰向来袭亚音速反舰导弹的航线进行垂直切入移动
        usv = side.get_unit_by_guid('cc4ceb53-9577-40c0-8898-2eed4a496828')
        ARAS = None
        ARAS = [contact for contact in side.contacts.values()
                if contact.m_ContactType == 1 and contact.fCurrentSpeed < 662]
        if ARAS:
            b = ARAS[0].fCurrentHeading
            a = geo.get_azimuth((ARAS[0].dLatitude, ARAS[0].dLongitude), (usv.dLatitude, usv.dLongitude))
            if a-b > 0:
                heading = geo.normal_angle(b - 90)
            else:
                heading = geo.normal_angle(b + 90)
            usv.plot_course([geo.get_end_point((usv.dLatitude, usv.dLongitude), 10, heading)])

        # 在超音速反舰导弹接近后使用更为激进的拦截策略
        ddg = side.get_unit_by_guid('4813e672-3d30-46e0-b570-93306dc6985c')
        if ddg:
            for contact in side.contacts.values():
                d = geo.get_horizontal_distance((contact.dLatitude, contact.dLongitude), (ddg.dLatitude, ddg.dLongitude))
                if d <= 92.6 and self.flag:
                    self.side_doct.set_weapon_release_authority('1310', '2200', 2, 1, 'inherit', 'inherit', 'false')
                    self.side_doct.set_weapon_release_authority('1310', '2203', 2, 1, 'inherit', 'inherit', 'false')
                    self.flag = False
